﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ADECMS.UserPages
{
    public partial class Search : System.Web.UI.Page
    {
        ADECMSDBDataContext cmsdb;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmsdb = new ADECMSDBDataContext();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            if (!Page.IsPostBack)
            {

                HttpCookie loginCookie = Request.Cookies["AuthCookie"];
                if (Request.IsAuthenticated)
                {
                    FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(loginCookie.Value);
                    Label1.Text = "Logovani ste " + ticket.Name.ToString();

                    var useri = from k in cmsdb.USERs
                                from p in cmsdb.USER_TYPEs
                                where k.username == ticket.Name && k.user_type_id == p.id && p.type == "Administrator"
                                select k;
                    try
                    {
                        USER kor = useri.First();
                        MenuItem apanel = new MenuItem("Admin panel");
                        Menu1.Items.Add(apanel);

                    }
                    catch (System.InvalidOperationException)
                    {
                        return;
                    }
                }
            }
        }

        protected void ButtonTrazi_Click(object sender, EventArgs e)
        {
            LinqDataSource1.DataBind();
            LinqDataSource1.Select.Contains(TextBoxTrazi.Text);
            ListView1.DataBind();
            
        }
        protected void UcitajTekst(String nasl)
        {
            try
            {
                NEW nov = cmsdb.NEWs.Single(n => n.title == nasl);
                ClanakText.Text = nov.text;
                ClanakAutorDat.Text = nov.USER.username + ", " + nov.news_date;
                ClanakAutorDat.Text += "<hr />";
            }
            catch (Exception)
            {
            }
        }

        protected void SignOut_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            Response.Redirect("/Default.aspx");
        }

        protected void AdminPanel_Click(object sender, EventArgs e)
        {

        }

        protected void DodajNovost_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/AddNews.aspx");
        }

        protected void ButtonPregledProfila_Click(object sender, EventArgs e)
        {

        }

        protected void MojProfil_Click(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

        }

        protected void ButtonPretraga_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Search.aspx");
        }

        protected void ButtonBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/UserPages/Start.aspx");
        }

        protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "BtnClick")
            {
                ClanakNaslov.Text = e.CommandArgument.ToString();
                UcitajTekst(ClanakNaslov.Text);

            }
        }

        protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            ClanakNaslov.Text = "";
            ClanakText.Text = "";
            ClanakAutorDat.Text = "";
            try
            {
                if (CheckBoxNaslov.Checked)
                {
                    if (CheckBoxKategorija.Checked)
                        e.Result = cmsdb.NEWs.Where(n => (n.text.Contains(TextBoxTrazi.Text) || n.title.Contains(TextBoxTrazi.Text))
                            && n.CATEGORY.name == DropDownList1.SelectedItem.ToString());
                    else
                        e.Result = cmsdb.NEWs.Where(n => (n.text.Contains(TextBoxTrazi.Text) || n.title.Contains(TextBoxTrazi.Text)));
                }
                else
                {
                    if (CheckBoxKategorija.Checked)
                        e.Result = cmsdb.NEWs.Where(n => n.text.Contains(TextBoxTrazi.Text) && n.CATEGORY.name == DropDownList1.SelectedItem.ToString());
                    else
                        e.Result = cmsdb.NEWs.Where(n => n.text.Contains(TextBoxTrazi.Text));
                }
            }
            catch (Exception)
            {
            }
        }

        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
        {
            switch (e.Item.Text)
            {
                case "Sign out":
                    FormsAuthentication.SignOut();
                    Response.Redirect("/Default.aspx");
                    break;
                case "Dodaj novost":
                    Response.Redirect("/UserPages/AddNews.aspx");
                    break;
                case "Pretraga":
                    Response.Redirect("/UserPages/Search.aspx");
                    break;
                case "Admin panel":
                    Response.Redirect("/UserPages/AdminPanel.aspx");
                    break;
                case "File upload":
                    Response.Redirect("/UserPages/FileUpload.aspx");
                    break;
                case "File download":
                    Response.Redirect("/UserPages/FileDownload.aspx");
                    break;
                case "Upload prezentacije":
                    Response.Redirect("/UserPages/PresentationUpload.aspx");
                    break;
                case "Pregled prezentacije":
                    Response.Redirect("/UserPages/PresentationView.aspx");
                    break;
                case "Pregledaj profil":
                    Response.Redirect("/UserPages/ProfileView.aspx");
                    break;
                case "Edituj profil":
                    Response.Redirect("/UserPages/ProfileEdit.aspx");
                    break;
            }
        }
    }
}
